<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="人员类别" prop="personType">
        <el-select v-model="queryParams.personType"  placeholder="请选择人员类别" clearable>
          <el-option
            v-for="dict in dict.type.pm_person_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="姓名" prop="personName">
        <el-input
          v-model="queryParams.personName"
          placeholder="请输入姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="锁定状态" prop="locked">
        <el-select v-model="queryParams.locked" placeholder="请选择锁定状态" clearable>
          <el-option
            v-for="dict in dict.type.pm_person_lock"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="职称" prop="title">
        <el-select v-model="queryParams.title" placeholder="请选择职称" clearable>
          <el-option
            v-for="dict in dict.type.pm_person_title"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <!-- 以下为高级查询 -->
      <el-form-item label="在职状态" prop="status" v-show="showHighSearch">
        <el-select v-model="queryParams.status" placeholder="请选择状态" clearable>
          <el-option
            v-for="dict in dict.type.pm_person_work_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="入职日期" v-show="showHighSearch">
        <el-date-picker
          v-model="daterangeJoinTime"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="离职日期" v-show="showHighSearch">
        <el-date-picker
          v-model="daterangeLeaveTime"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="缴社保日" v-show="showHighSearch">
        <el-date-picker
          v-model="daterangeSocialInsuranceTime"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="隶属性质" prop="externalUnits" v-show="showHighSearch">
        <el-select v-model="queryParams.externalUnits" placeholder="请选择隶属性质" clearable>
          <el-option
            v-for="dict in dict.type.pm_person_external"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="专业" prop="personMajor" v-show="showHighSearch">
        <el-select v-model="queryParams.personMajor"  placeholder="请选择专业" clearable>
          <el-option
            v-for="dict in dict.type.pm_major"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button type="success" icon="el-icon-search" size="mini" @click='showHighSearch=!showHighSearch'>高级搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['business:project_person:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['business:project_person:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['business:project_person:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['business:project_person:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="project_personList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />

      <el-table-column label="姓名" align="center" prop="personName" />
      <el-table-column label="人员类别" align="center" prop="personType" width="280">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.pm_person_type" :value="scope.row.personType ? scope.row.personType.split(',') : []"/>
        </template>
      </el-table-column>
      <el-table-column label="职称" align="center" prop="title">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.pm_person_title" :value="scope.row.title"/>
        </template>
      </el-table-column>
      <el-table-column label="人员专业" align="center" prop="personMajor" width="230">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.pm_major" :value="scope.row.personMajor ? scope.row.personMajor.split(',') : []"/>
        </template>
      </el-table-column>
      <el-table-column label="隶属性质" align="center" prop="externalUnits">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.pm_person_external" :value="scope.row.externalUnits"/>
        </template>
      </el-table-column>
      <el-table-column label="人员锁定状态" align="center" prop="locked" width="180">
        <template slot-scope="scope">
          <el-link v-if="scope.row.locked" @click="handlePerson(scope.row)" type="primary">锁定详情</el-link>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.pm_person_work_status" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="入职日期" align="center" prop="joinTime" width="100">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.joinTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="缴社保日" align="center" prop="socialInsuranceTime" width="100">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.socialInsuranceTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="离职日期" align="center" prop="leaveTime" width="100">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.leaveTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>


      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="180">
        <template slot-scope="scope">
          <router-link :to="'/business/person_certificate-index/index/' + scope.row.personId" class="link-type">
            <span style="color:#46a6ff;font-size:12px;margin-right:10px;">证书管理</span>
          </router-link>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['business:project_person:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['business:project_person:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改基本资料管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="950px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="150px">
        <el-row>
          <el-col :span="12">
        <el-form-item label="姓名" prop="personName" style="width: 370px">
          <el-input v-model="form.personName" placeholder="请输入姓名" />
        </el-form-item>
          </el-col>
          <el-col :span="12">
        <el-form-item label="状态" prop="status" >
          <el-radio-group v-model="form.status">
            <el-radio
              v-for="dict in dict.type.pm_person_work_status"
              :key="dict.value"
              :label="dict.value"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
        <el-form-item label="入职日期" prop="joinTime">
          <el-date-picker clearable
            v-model="form.joinTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择入职日期">
          </el-date-picker>
        </el-form-item>
          </el-col>
          <el-col :span="12">
        <el-form-item label="离职日期" prop="leaveTime">
          <el-date-picker clearable
            v-model="form.leaveTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择离职日期">
          </el-date-picker>
        </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
        <el-form-item label="缴社保日" prop="socialInsuranceTime">
          <el-date-picker clearable
            v-model="form.socialInsuranceTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择缴社保日">
          </el-date-picker>
        </el-form-item>
          </el-col>
          <el-col :span="12">
        <el-form-item label="职称">
          <el-radio-group v-model="form.title">
            <el-radio
              v-for="dict in dict.type.pm_person_title"
              :key="dict.value"
              :label="dict.value"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
        <el-form-item label="项目经理隶属性质" prop="externalUnits">
          <el-select v-model="form.externalUnits" placeholder="请选择隶属性质">
            <el-option
              v-for="dict in dict.type.pm_person_external"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
          </el-col>
          <el-col :span="12">
        <el-form-item label="人员专业">
          <el-checkbox-group v-model="form.personMajor">
            <el-checkbox
              v-for="dict in dict.type.pm_major"
              :key="dict.value"
              :label="dict.value">
              {{dict.label}}
            </el-checkbox>
          </el-checkbox-group>
        </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
        <el-form-item label="人员类别" prop="personType">
          <el-checkbox-group v-model="form.personType">
            <el-checkbox
              v-for="dict in dict.type.pm_person_type"
              :key="dict.value"
              :label="dict.value">
              {{dict.label}}
            </el-checkbox>
          </el-checkbox-group>
        </el-form-item>
          </el-col>
        </el-row>
        <!-- <el-divider content-position="center">证书管理信息</el-divider>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAddPersonCertificate">添加</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="danger" icon="el-icon-delete" size="mini" @click="handleDeletePersonCertificate">删除</el-button>
          </el-col>
        </el-row>
        <el-table :data="personCertificateList" :row-class-name="rowPersonCertificateIndex" @selection-change="handlePersonCertificateSelectionChange" ref="personCertificate">
          <el-table-column type="selection" width="50" align="center" />
          <el-table-column label="序号" align="center" prop="index" width="50"/>
          <el-table-column label="人员名称" prop="personName" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.personName" placeholder="请输入人员名称" />
            </template>
          </el-table-column>
          <el-table-column label="证书名称" prop="certificateName" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.certificateName" placeholder="请输入证书名称" />
            </template>
          </el-table-column>
          <el-table-column label="签发日期" prop="issueDate" width="240">
            <template slot-scope="scope">
              <el-date-picker clearable v-model="scope.row.issueDate" type="date" value-format="yyyy-MM-dd" placeholder="请选择签发日期" />
            </template>
          </el-table-column>
          <el-table-column label="有效日期" prop="expirationDate" width="240">
            <template slot-scope="scope">
              <el-date-picker clearable v-model="scope.row.expirationDate" type="date" value-format="yyyy-MM-dd" placeholder="请选择有效日期" />
            </template>
          </el-table-column>
        </el-table> -->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <el-dialog title="人员锁定详情" :visible.sync="personProjectOpen" width="750px" append-to-body>
      <el-table :data="personProjectList" :row-class-name="personProjectListIndex">

          <el-table-column label="人员名称" prop="personName" width="150"></el-table-column>
          <el-table-column label="人员类别" prop="personType" width="150">
            <template slot-scope="scope">
              <dict-tag :options="dict.type.pm_person_type" :value="scope.row.personType ? scope.row.personType.split(',') : []"/>
            </template>
          </el-table-column>
          <el-table-column label="项目名称" prop="projectName" width="150"></el-table-column>
          <el-table-column label="项目类别" prop="projectType" width="240">
            <template slot-scope="scope">
              <span>{{ scope.row.projectType==1?"业绩(历史)项目":"投标项目" }}</span>
            </template>
          </el-table-column>
        </el-table>
    </el-dialog>

  </div>
</template>

<script>
import { listProject_person, getProject_person, delProject_person, addProject_person, check_person_name,updateProject_person } from "@/api/business/project_person";
import {  getPersonProject} from "@/api/business/current_project";

export default {
  name: "Project_person",
  dicts: ['pm_major', 'pm_person_title', 'pm_person_external', 'pm_person_work_status', 'pm_person_type','pm_person_lock'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 子表选中数据
      checkedPersonCertificate: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      //高级搜索
      showHighSearch: false,
      // 总条数
      total: 0,
      // 基本资料管理表格数据
      project_personList: [],
      //人员占用
      personProjectList: [],
      // 证书管理表格数据
      personCertificateList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      personProjectOpen: false,
      // 证书图片时间范围
      daterangeJoinTime: [],
      // 证书图片时间范围
      daterangeLeaveTime: [],
      // 证书图片时间范围
      daterangeSocialInsuranceTime: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        personName: null,
        status: null,
        joinTime: null,
        leaveTime: null,
        socialInsuranceTime: null,
        title: null,
        externalUnits: null,
        personType: null,
        personMajor: null,
        majorList: null,
        personTypeList: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        personName: [
          { required: true, validator: (rule, value, callback) => {
            if (!value) {
              callback(new Error('姓名不能为空'))
            } else {
              check_person_name(value).then(response => {
                if(response.data!=null && response.data!=this.form.personId){
                  callback(new Error("名称重复,请重新输入"));
                }else{
                  callback();
                }
              });
            }
          }, trigger: ["blur","change"] }
        ],
        status: [
          { required: true, message: "状态不能为空", trigger: "blur" }
        ],
        /*joinTime: [
          { required: true, message: "入职日期不能为空", trigger: "blur" }
        ],*/
        personType: [
          { required: true, message: "人员类别不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询基本资料管理列表 */
    getList() {
      this.loading = true;
      this.queryParams.params = {};
      if (null != this.daterangeJoinTime && '' != this.daterangeJoinTime) {
        this.queryParams.params["beginJoinTime"] = this.daterangeJoinTime[0];
        this.queryParams.params["endJoinTime"] = this.daterangeJoinTime[1];
      }
      if (null != this.daterangeLeaveTime && '' != this.daterangeLeaveTime) {
        this.queryParams.params["beginLeaveTime"] = this.daterangeLeaveTime[0];
        this.queryParams.params["endLeaveTime"] = this.daterangeLeaveTime[1];
      }
      if (null != this.daterangeSocialInsuranceTime && '' != this.daterangeSocialInsuranceTime) {
        this.queryParams.params["beginSocialInsuranceTime"] = this.daterangeSocialInsuranceTime[0];
        this.queryParams.params["endSocialInsuranceTime"] = this.daterangeSocialInsuranceTime[1];
      }
      listProject_person(this.queryParams).then(response => {
        this.project_personList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        personId: null,
        personName: null,
        status: "0",
        joinTime: null,
        leaveTime: null,
        socialInsuranceTime: null,
        title: "0",
        externalUnits: "0",
        personType: [],
        personMajor: [],
        creator: null,
        createdTime: null,
        updateBy: null,
        updatedTime: null
      };
      this.personCertificateList = [];
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeJoinTime = [];
      this.daterangeLeaveTime = [];
      this.daterangeSocialInsuranceTime = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.personId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加人员资料";
    },
    /** 人员占用按钮操作 */
    handlePerson(row) {

      const personId = row.personId
      getPersonProject(personId).then(response => {
        this.personProjectList = response.rows;
        this.personProjectOpen = true;
      });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const personId = row.personId || this.ids
      getProject_person(personId).then(response => {
        this.form = response.data;
        this.form.personType = this.form.personType ? this.form.personType.split(",") : [];
        this.form.personMajor = this.form.personMajor ? this.form.personMajor.split(",") : [];
        this.personCertificateList = response.data.personCertificateList;
        this.open = true;
        this.title = "修改人员资料";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.personType = this.form.personType.join(",");
          this.form.personMajor = this.form.personMajor.join(",");
          this.form.personCertificateList = this.personCertificateList;
          if (this.form.personId != null) {
            updateProject_person(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addProject_person(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const personIds = row.personId || this.ids;
      this.$modal.confirm('是否确认删除"' + row.personName + '"？').then(function() {
        return delProject_person(personIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
	/** 证书管理序号 */
    rowPersonCertificateIndex({ row, rowIndex }) {
      row.index = rowIndex + 1;
    },
    /** 证书管理添加按钮操作 */
    handleAddPersonCertificate() {
      let obj = {};
      obj.personName = "";
      obj.certificateName = "";
      obj.issueDate = "";
      obj.expirationDate = "";
      obj.imagePath = "";
      this.personCertificateList.push(obj);
    },
    /** 证书管理删除按钮操作 */
    handleDeletePersonCertificate() {
      if (this.checkedPersonCertificate.length == 0) {
        this.$modal.msgError("请先选择要删除的证书管理数据");
      } else {
        const personCertificateList = this.personCertificateList;
        const checkedPersonCertificate = this.checkedPersonCertificate;
        this.personCertificateList = personCertificateList.filter(function(item) {
          return checkedPersonCertificate.indexOf(item.index) == -1
        });
      }
    },
    /** 复选框选中数据 */
    handlePersonCertificateSelectionChange(selection) {
      this.checkedPersonCertificate = selection.map(item => item.index)
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('business/project_person/export', {
        ...this.queryParams
      }, `project_person_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
